// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const { action, data } = event
  const openid = wxContext.OPENID
  let result = {}
  switch (action) {
    case 'init_buy': result.data = await init_buy(); break;
    case 'get_goods': result.data = await get_goods(data); break;
    case 'add_order': result.data = await add_order(openid, data); break;
    case 'goods_list': result.data = await goods_list(); break;
    case 'add_dry': result.data = await add_dry(openid, data); break;
    case 'get_order': result.data = await get_order(openid); break;
    case 'add_user': result.data = await add_user(openid, data); break;
    case 'get_list': result.data = await get_list(data); break;
    case 'push_asso': result.data = await push_asso(openid, data); break;
    case 'get_asso': result.data = await get_asso(); break;
  }
  return result
}
// async function get_asso() {
//   const asso = await db.collection("asso").orderBy('_id', 'desc').limit(1000).get().then(res => { return res.data })
//   return {
//     code: 200,
//     data: {
//       asso
//     }
//   }
// }
async function get_asso() {  
  const res = await db.collection("asso")  
      .orderBy('_id', 'desc')  
      .limit(1000)  
      .get();  
  const asso = res.data;  
  return {  
      code: 200,  
      data: {  
          asso  
      }  
  };  
}
async function push_asso(openid, data) {
  const {
    imgs,
    content,
    args
  } = data
  const asso = await db.collection("asso").add({
    data: {
      imgs,
      content,
      openid,
      nickName: args.nickName,
      avatarUrl: args.avatarUrl
    }
  }).then(res => { return res })
  return {
    code: 200,
    mes: "发布成功",
    data: {
      asso
    }
  }
}
async function get_list(data) {
  const { type } = data
  const list = await db.collection(type).get().then(res => { return res.data })
  return {
    code: 200,
    data: {
      list
    }
  }
}
async function add_user(openid, data) {
  const {
    avatarUrl,
    nickName,
    reg_time
  } = data
  const user = await db.collection("user").doc(openid).set({
    data: {
      openid,
      avatarUrl,
      nickName,
      reg_time
    }
  }).then(res => {
    return res
  })
  return {
    code: 200,
    msg: "保存成功",
    data: {
      user,
      args: {
        openid, avatarUrl, nickName
      }
    }
  }
}
async function get_order(openid) {
  const order = await db.collection("order").where({ openid }).get().then(res => { return res.data })
  const dry = await db.collection("dry").where({ openid }).get().then(res => { return res.data })
  return {
    code: 200,
    data: {
      order, dry
    }
  }
}
async function add_dry(openid, data) {
  const {
    car,
    name,
    phone
  } = data
  const dry = await db.collection("dry").where({ phone, car, status: '预约中' }).get().then(res => { return res.data.length })
  if (dry != 0) {
    return {
      code: 400,
      msg: '您已预约该车型'
    }
  }
  else {
    const req = await db.collection("dry").add({
      data: {
        car,
        name,
        phone,
        openid,
        status: '预约中'
      }
    }).then(res => { return res })
    return {
      code: 200,
      msg: "申请中，请等待"
    }
  }
}
async function goods_list() {
  const goods = await db.collection("goods").limit(1000).get().then(res => { return res.data })
  let list = goods.map(item => { return item.goods_name })
  return {
    code: 200,
    data: {
      list
    }
  }
}
async function add_order(openid, data) {
  const {
    imgs_mess,
    name,
    card,
    phone,
    way,
    total,
    goods,
    pay_date
  } = data
  const {
    zheng,
    water,
    hun,
    card_front,
    card_back
  } = imgs_mess
  let all_path = []
  let current_path = 0
  if (way == '全款') {
    all_path = [
      {
        text: '支付定金',
      },
      {
        text: '生产中',
      },
      {
        text: '车辆到店',
      },
      {
        text: '车辆交付',
      }
    ]
    current_path = goods.is_car == false ? 1 : 2
  }
  else {
    all_path = [
      {
        text: '贷款申请',
      },
      {
        text: '贷款通过',
      },
      {
        text: '生产中',
      },
      {
        text: '车辆到店',
      },
      {
        text: '车辆交付',
      }
    ]
    current_path = 0
  }
  const order = await db.collection("order").add({
    data: {
      name,
      card,
      phone,
      total,
      way,

      zheng,
      water,
      hun,
      card_front,
      card_back,

      all_path,
      current_path,
      pay_date,

      goods_id: goods._id,
      goods_name: goods.goods_name,
      goods_imgs: goods.goods_imgs,
      goods,
      openid
    }
  }).then(res => { return res })
  return {
    code: 200,
    msg: '支付成功'
  }
}
async function get_goods(data) {
  const {
    type_id
  } = data
  const goods = await db.collection("goods").where({ type_id }).limit(1000).get().then(res => { return res.data })
  return {
    code: 200,
    data: {
      goods
    }
  }
}
async function init_buy() {
  const type = await db.collection("type").limit(100).get().then(res => { return res.data })
  const goods = await db.collection("goods").where({ type_id: type[0]._id }).limit(1000).get().then(res => { return res.data })
  return {
    code: 200,
    data: {
      type,
      goods
    }
  }
}